package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import connection.ServiceProxy;
import java.util.regex.*;

/**
 * Servlet implementation class signUp
 */
public class signUp extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public signUp() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String firstName = request.getParameter("firstName");
		String lastName = request.getParameter("lastName");
		String emailId = request.getParameter("emailId");
		String password = request.getParameter("password");
		String confirmPassword = request.getParameter("confirmPassword");
		
		String destination = null;
		String result = null;
		
		String message = null;
		boolean error = false;
		
		if(firstName.equals("")) {
			message = "Please provide your First Name";
			error = true;
		}
		else if(lastName.equals("")){
			message = "Please provide your Last Name";
			error = true;
		}
		else if(emailId.equals("")){
			message = "Please provide an Email Id";
			error = true;
		}
		else if(!isValidEmailAddress(emailId)) {
			message = "Invalid Email Id format";
			error = true;
		}
		else if(password.equals("")){
			message = "Please provide a password";
			error = true;
		}
		else if(!password.equals(confirmPassword)){
			message = "Password and Confirm Password do not match";
			error = true;
		}
		else {
			ServiceProxy oProxy = new ServiceProxy();
			result = oProxy.signUp(emailId, password, firstName, lastName);
			
			if(result.equals("true")) {
				
				HttpSession session = request.getSession(true);
				session.setAttribute("authenticated", "true");
				
				result = oProxy.signIn(emailId, password);
								
				String userProfile[] = result.split("#");
				session.setAttribute("userId", userProfile[0]);
				session.setAttribute("emailId", userProfile[1]);
				session.setAttribute("firstName", userProfile[2]);
				session.setAttribute("lastName", userProfile[3]);
				session.setAttribute("lastLoginTime", userProfile[4] + "PST");
				
							
				message = "Welcome to Market Place";
			}
			else {
				error = true;
				if(result.equals("false")) {
					message = "Please try again later";
				}
				else
					message = result;
			}
		}
		
		request.setAttribute("message", message);
		
		if(error == true) {
			destination = "/signUp.jsp";
		}
		else
			destination = "/home";
		
		

		RequestDispatcher rd = this.getServletContext().getRequestDispatcher(destination);
		rd.forward(request, response);
		
	}
	
	private boolean isValidEmailAddress(String aEmailAddress){
		
	      //Set the email pattern string
	      Pattern p = Pattern.compile("[a-zA-Z]*[0-9]*@[a-zA-Z]*.[a-zA-Z]*");

	      //Match the given string with the pattern
	      Matcher m = p.matcher(aEmailAddress);

	      //check whether match is found 
	      return m.matches();	      
	   
	}

}
